package io.renren.modules.weixin.api;

import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.BeanUtil;
import io.renren.modules.weixin.api.response.BaseResponse;
import io.renren.modules.weixin.api.response.CreateGroupResponse;
import io.renren.modules.weixin.api.response.GetUserInfoResponse;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zhangxinlin
 * @Description:
 * @Date: Create in 17:08 2018/2/6
 * @Modified By：
 */
@Service
public class UserAPI extends BaseAPI {

    /**
     * @Author: zhangxinlin
     * @Description: 获取用户信息
     * @Date: 15:17 2018/2/8
     */
    public GetUserInfoResponse getUserInfo(String openid){
        GetUserInfoResponse response;
        logger.info("获取用户信息，openid:{}",openid);
        String url = BASE_API_URL + "cgi-bin/user/info?access_token=#&lang=zh_CN&openid=" + openid;
        GetUserInfoResponse r = executeGet(url,GetUserInfoResponse.class);
        if(r == null || !r.isSuccess()){
            logger.info("获取用户信息失败，result:{}",r.toString());
        }
        return r;
    }

    /**
     * @Author: zhangxinlin
     * @Description: 创建分组
     * @Date: 17:17 2018/2/8
     */
    public CreateGroupResponse createGroup(String grouName){
        String url = BASE_API_URL + "cgi-bin/groups/create?access_token=#";
        Map<String, Object> param = new HashMap<String, Object>();
        Map<String, Object> group = new HashMap<String, Object>();
        group.put("name", grouName);
        param.put("group", group);
        CreateGroupResponse r = executePost(url, JSONObject.toJSONString(param),CreateGroupResponse.class);
        if(r == null || !r.isSuccess()){
            logger.info("创建分组失败，result:{}",r.toString());
        }
        return r;
    }


}
